﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TenderSampleApp.Models.Profile;
using System.Web.Profile;
using System.Web.Security;
using TenderSampleApp.Code;
using System.IO;
using TenderSampleApp.Areas.RAC.Models;

namespace TenderSampleApp.Areas.RAC.Controllers
{
    public class RACProfileController : Controller
    {
        //
        // GET: /Profile/

        //
        //Set user profile

 

        [Authorize(Roles = "RAC")]
        public ActionResult GetRACProfile()
        {
            ProfileBase profile = ProfileBase.Create(User.Identity.Name);
            string FirstName = profile.GetPropertyValue("FirstName").ToString();
            string LastName = profile.GetPropertyValue("LastName").ToString();
            string Surname = profile.GetPropertyValue("Surname").ToString();
            string Email = profile.GetPropertyValue("Email").ToString();
            string Phone = profile.GetPropertyValue("Phone").ToString();
            string PhysicalRegisterNumber = profile.GetPropertyValue("PhysicalRegisterNumber").ToString();
            string JuridicalRegisterNumber = profile.GetPropertyValue("JuridicalRegisterNumber").ToString();
            string RequestPointNumber = profile.GetPropertyValue("RequestPointNumber").ToString();
            string RegionCode = profile.GetPropertyValue("RegionCode").ToString();

            RACProfileModel rpm = new RACProfileModel() { Email=Email, FirstName=FirstName, JuridicalRegisterNumber=JuridicalRegisterNumber, PhysicalRegisterNumber=PhysicalRegisterNumber, RequestPointNumber= RequestPointNumber, LastName = LastName, Phone=Phone, RegionCode=RegionCode, Surname= Surname };
            return View(rpm);
        }

        //Get Photo for profile
        [Authorize(Roles = "RAC")]
        public FileContentResult ReturnPhotoFromProfile()
        {
            ProfileBase profile = ProfileBase.Create(User.Identity.Name);
            byte[] Photo = profile.GetPropertyValue("Photo") as byte[];
            return new FileContentResult(Photo, "image/png");
        }

        #region RACProfileAction
        [HttpGet]
        [Authorize(Roles = "RAC")]
        public ActionResult FillRACProfile()
        {
           
            string[]users=Roles.GetUsersInRole("RAC");
            List<string>listPhysRegNum= new List<string>();
            List<string> listJurRegNum = new List<string>();
            foreach(string username in users)
            {
                ProfileBase profile = ProfileBase.Create(username);
                string physRegNum = profile.GetPropertyValue("PhysicalRegisterNumber")!=null ? profile.GetPropertyValue("PhysicalRegisterNumber").ToString():null;
                if(!String.IsNullOrEmpty(physRegNum))
                 listPhysRegNum.Add(physRegNum);

                string jurRegNum = profile.GetPropertyValue("JuridicalRegisterNumber") != null ? profile.GetPropertyValue("JuridicalRegisterNumber").ToString() : null;
                if (!String.IsNullOrEmpty(jurRegNum))
                    listJurRegNum.Add(jurRegNum);
            }
            bool IsNotExist = false;
            string physnum="";
            while (!IsNotExist)
            {
                Random rand = new Random();
                physnum = rand.Next(1,50).ToString();
                if (!listPhysRegNum.Contains(physnum))
                    IsNotExist = true;
            }

            IsNotExist = false;
            string jurnum = "";
            while (!IsNotExist)
            {
                Random rand = new Random();
                jurnum = rand.Next(51,100).ToString();
                if (!listPhysRegNum.Contains(jurnum))
                    IsNotExist = true;
            }
            if (jurnum.Length == 1)
                jurnum = "0" + jurnum;
            if (physnum.Length == 1)
                physnum = "0" + physnum;
           MembershipUser user = Membership.GetUser();
           RACProfileModel rpm = new RACProfileModel() { PhysicalRegisterNumber = physnum, JuridicalRegisterNumber = jurnum, Email = user.Email };

            return View(rpm);
        }

        [HttpPost]
        [Authorize(Roles = "RAC")]
        public ActionResult FillRACProfile(RACProfileModel racprofile, HttpPostedFileBase photo)
        {

           
           
            MembershipUser user = Membership.GetUser();
            ProfileBase profile = ProfileBase.Create(user.UserName, user.IsApproved);
            profile.SetPropertyValue("FirstName", racprofile.FirstName);
            profile.SetPropertyValue("Lastname", racprofile.LastName);
            profile.SetPropertyValue("Surname", racprofile.Surname);
            profile.SetPropertyValue("Email", racprofile.Email);
            profile.SetPropertyValue("Phone", racprofile.Phone);
            profile.SetPropertyValue("PhysicalRegisterNumber", racprofile.PhysicalRegisterNumber);
            profile.SetPropertyValue("JuridicalRegisterNumber", racprofile.JuridicalRegisterNumber);
            profile.SetPropertyValue("RegionCode", racprofile.RegionCode);
            profile.SetPropertyValue("RequestPointNumber", racprofile.RequestPointNumber);

            //Get byte
            if (photo != null)
            {
                Stream stream = photo.InputStream;
                byte[] buf = new byte[stream.Length];
                stream.Read(buf, 0, buf.Length);
                profile.SetPropertyValue("Photo", buf);
            }
            profile.Save();
            return RedirectToAction("GetRACProfile");
        }
  
        #endregion
      
    

    }
}
